/**
 * @file NDS03_Calibration_Example.c
 * @author tongsheng.tang
 * @brief 标定示例
 * @version 2.x.x
 * @date 2024-04
 * 
 * @copyright Copyright (c) 2024, Nephotonics Information Technology (Hefei) Co., Ltd.
 * 
 */

#include <stdio.h>
#include "nds03_platform.h"
#include "nds03_comm.h"
#include "nds03_dev.h"
#include "nds03_data.h"
#include "nds03_calib.h"
#include "nds03_def.h"

/** 模组设备 */
NDS03_Dev_t g_nds03_device = {
                .platform.i2c_dev_addr = NDS03_DEFAULT_SLAVE_ADDR, 
                .platform.xshut_pin = 0,
            };

/**
 * @brief NDS03标定函数示例
 *        按照NDS03标定手册进行标定，以手册为主
 *        注意：以下标定示例需要将设备处于500mm处
 * @return int32_t 
 */
int32_t main(void)
{
    NDS03_Error     ret;
    NDS03_Dev_t     *pNxDevice = &g_nds03_device;

    /* 测距次数为20次 */
    int32_t     i,  cnt = 20;
    int32_t     depth_sum, depth_aver;

    /* 初始化平台，包含I2C初始化 */
    if(NDS03_ERROR_NONE != nds03_platform_init(&pNxDevice->platform, NULL))
    {
        printf("nds03_platform init error\n");
        return NDS03_ERROR_API;
    }

    /* 初始化设备变量的I2C地址 */
    pNxDevice->platform.i2c_dev_addr = NDS03_DEFAULT_SLAVE_ADDR;

    /* 循环等待设备启动, 若模组或者I2C读写函数有问题则会报错 */
    if(NDS03_ERROR_NONE != NDS03_WaitDeviceBootUp(pNxDevice))
    {
        NX_PRINTF("NDS03_WaitDeviceBootUp error\r\n");
        return NDS03_ERROR_BOOT;
    }   

    /* 初始化模组设备 */
    if(NDS03_ERROR_NONE != NDS03_InitDevice(pNxDevice))
    {
        NX_PRINTF("NDS03_InitDevice error!!\r\n");
        return NDS03_ERROR_INIT;
    }

    /* 将设备处于500mm处 */
    /* 串扰标定 */
    ret = NDS03_XtalkCalibration(pNxDevice);
    if(NDS03_ERROR_NONE != ret)
    {
        NX_PRINTF("NDS03_XtalkCalibration error!!\r\n");
        return ret;
    }
    /* Offset标定 */
    ret = NDS03_OffsetCalibrationAtDepth(pNxDevice, 500);
    if(NDS03_ERROR_NONE != ret)
    {
        NX_PRINTF("NDS03_OffsetCalibrationAtDepth error!!\r\n");
        return ret;
    }

    /* 获取平均值 */
    depth_sum = 0;
    for(i = 0; i < cnt; i++) {
        if(NDS03_ERROR_NONE != NDS03_GetSingleRangingData(pNxDevice)){
            NX_PRINTF("NDS03_GetSingleRangingData error!!\r\n");
            return NDS03_ERROR_RANGING;
        }
        depth_sum = depth_sum + (int32_t)(uint32_t)pNxDevice->ranging_data[0].depth;
    }
    depth_aver = depth_sum / cnt;
    if(depth_aver < 480 || depth_aver > 520){
        NX_PRINTF("NDS03 calibration fail!!\r\n");
        return NDS03_ERROR_CALIB;
    }
    NX_PRINTF("NDS03 calibration success\r\n");

    /* 释放平台 */
    nds03_platform_uninit(pNxDevice, NULL);

    return 0;
}